package com.example.dangerouscargodriver.ui.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.baiju.netmanager.Http.IHttpRequest;
import com.baiju.style.citylist.Toast.ToastUtils;
import com.example.dangerouscargodriver.controller.AuthController;
import com.example.dangerouscargodriver.module.ResponseInfo;
import com.example.dangerouscargodriver.param.RemoteAPI;
import com.example.dangerouscargodriver.param.RemoteAPICmd;
import com.example.dangerouscargodriver.ui.fragment.HttpRequestFragment;
import com.example.dangerouscargodriver.widget.ActionBar;
import com.example.dangerouscargodriver.R;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;

public class EmployeeAddActivity extends HttpRequestActivity implements View.OnClickListener{

    private static final String TAG = "EmployeeAddActivity";
    private ActionBar mActionBar = null;
    private Button mAddEmployee = null;

    private EditText mTelephoneNo = null;
    private EditText mVerifyNo = null;
    private EditText mEmployeeName = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.initHttpComponent(EmployeeAddActivity.class);
        setContentView(R.layout.activity_employee_add);

        mAddEmployee = findViewById(R.id.btn_add_employee);
        mAddEmployee.setOnClickListener(this);

        mTelephoneNo = findViewById(R.id.ed_telephone_no);
        mVerifyNo = findViewById(R.id.ed_verify_no);
        mEmployeeName = findViewById(R.id.ed_employee_name);

        mActionBar = findViewById(R.id.actionBarAddEmployee);
        mActionBar.setOnBackClickListener( new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private boolean checkUserInput(){
        if(mTelephoneNo.getText().toString() == null ||
                mTelephoneNo.getText().toString().equals("") ||
                mVerifyNo.getText().toString() == null ||
                mVerifyNo.getText().toString().equals("") ||
                mEmployeeName.getText().toString() == null ||
                mEmployeeName.getText().toString().equals("")){
            return false;
        }
        else{
            return true;
        }
    }

    private void executeAddEmployeeAction(){
        HashMap<String, String> header = new HashMap<String, String>();
        header.put("token", AuthController.Instance().getToken());

        HashMap<String, String> param = new HashMap<String, String>();
        param.put("name", mEmployeeName.getText().toString());
        param.put("code", mVerifyNo.getText().toString());
        param.put("account", mTelephoneNo.getText().toString());

        sendHttpFormRequest(
                RemoteAPI.REQUEST_V1_CHILD_ACCOUNT,
                header,
                param,
                RemoteAPICmd.REQUEST_V1_CHILD_ACCOUNT);
    }

    @Override
    public void onClick(View v) {
        if(v.getId() == R.id.btn_add_employee){
            if(checkUserInput()) {
                executeAddEmployeeAction();
            }
            else{
                ToastUtils.showLongToast(this, "请填写完整信息");
            }
        }
    }

    @Override
    protected void onHttpResponse(int actionId, String response) {
        try {
            JSONObject res = new JSONObject(response);
            Gson gson = new Gson();
            if(actionId == RemoteAPICmd.REQUEST_V1_CHILD_ACCOUNT){
                Log.d(TAG, "onResponse: " + response);
                ResponseInfo responseInfo = gson.fromJson(response, ResponseInfo.class);
                ToastUtils.showLongToast(this, res.getString("message"));
                if(responseInfo != null && responseInfo.getStatus() == 1){
                    finish();
                }
                else{
                    /**/
                }
            }
            else{

            }
        }
        catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onHttpErrorResponse(int actionId, String response) {
        if(actionId == RemoteAPICmd.REQUEST_V1_CHILD_ACCOUNT){
            noticeOffline();
        }
        else {

        }
    }
}